<?php
/**
 * Created by PhpStorm.
 * User: Administrator
 * Date: 2018/12/11/0011
 * Time: 9:08
 */

namespace app\sports_admin\controller;


use app\exception\EquipmentException;
use app\sports_admin\model\WatchInstructionsRecord as WatchInstructionsRecordModel;
use think\Request;
use app\sports_admin\model\Equipment as EquipmentModel;
use app\sports_admin\service\Equipment as EquipmentService;
use app\sports_admin\vaildate\Equipment as EquipmentVaildate;
use app\sports_admin\model\School as SchoolModel;
use app\sports_admin\model\Parents as ParentsModel;
class Equipment extends Base
{
    protected $beforeActionList = [
        'isResource'=>['only'=>'index,equipmentlist,add,insertequipment,edit,updateequipment,deleteequipment,
                        binding,untying,parentsuntying,schooluntying,importequipmentinfo,status'],
        'operationSelect'=>['only'=>'index,add,edit,status'],
        'operationUpdate'=>['only'=>'updateequipment,binding,untying,parentsuntying,schooluntying'],
        'operationInsert'=>['only'=>'insertequipment,importequipmentinfo'],
        'operationDelete'=>['only'=>'deleteequipment']
    ];
    //设备管理首页
    public function index()
    {
        $school = SchoolModel::field('school_id,school_name')->select();
        return view('index',compact('school'));
    }
    /**
     * 获取设备列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $searchName 要检索的字段
     */
    public function equipmentList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','searchName','school_id']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $searchName = trim($params['searchName']);
        $schoolId = $params['school_id'];
        return EquipmentModel::getEquipmentList($page,$rows,$sort,$sortOrder,$searchName,$schoolId);
    }
    /**
     * 添加设备
    */
    public function add()
    {
        $parent = ParentsModel::field('id,phone')->select();
        $school = SchoolModel::field('school_id,school_name')->select();
        return view('add',compact('parent','school'));
    }
    /**
     * 添加操作
    */
    public function insertEquipment(Request $request)
    {
        if($request->isAjax()){
            (new EquipmentVaildate())->insert();
            return EquipmentService::equipmentInsert();
        }
    }
    /**
     * 编辑页面
     * @param $id 设备信息id
    */
    public function edit(Request $request)
    {
        $id = intval($request->get('id'));
        $numberInfo = EquipmentModel::where('id',$id)->field('parent_id,school_id,id,number,serial_number,batch')->find();
        $parent = ParentsModel::field('id,phone')->select();
        $school = SchoolModel::field('school_id,school_name')->select();
        return view('edit',compact('numberInfo','parent','school'));
    }
    /**
     * 更新设备信息
     */
    public function updateEquipment(Request $request)
    {
        if($request->isAjax()){
            (new EquipmentVaildate())->update();
            return EquipmentService::updateEquipment();
        }
    }
    /**
     * 删除设备信息
    */
    public function deleteEquipment(Request $request)
    {
        if($request->isAjax()){
            (new EquipmentVaildate())->go_check();
            $id = $request->post('id');
            $bool = EquipmentModel::destroy($id);
            if(!$bool){
                throw new EquipmentException([
                    'msg'=>'删除失败',
                    'errorCode'=>'删除成功'
                ]);
            }
            return json(['code'=>202,'msg'=>'删除成功']);
        }
    }
    /**
     * 设备信息绑定
     */
    public function binding(Request $request)
    {
        if($request->isAjax()){
            (new EquipmentVaildate())->binding();
            return EquipmentService::equipmentBinding();
        }
    }
    /**
     * 设备信息解绑
     */
    public function untying(Request $request)
    {
        if($request->isAjax()){
            (new EquipmentVaildate())->untying();
            return EquipmentService::untying();
        }
    }
    /**
     * 家长解绑
    */
    public function parentsUntying(Request $request)
    {
        if($request->isAjax()){
            return EquipmentService::parentUntying();
        }
    }
    /**
     * 学校解绑
    */
    public function schoolUntying(Request $request)
    {
        if($request->isAjax()){
            return EquipmentService::schoolUntying();
        }
    }
    /**
     * 批量导入设备信息
    */
    public function importEquipmentInfo(Request $request)
    {
        if($request->isAjax()){
            return EquipmentService::importEquipmentInfo();
        }
    }
    /**
     * 设备状态
     * @param $number 设备编号
     */
    public function status()
    {
        $number = trim(input('get.number'));
        return view('status',compact('number'));
    }
    /**
     * 设备通讯记录
     * @param $number 设备编号
     */
    public function communication()
    {
        $number = trim(input('get.number'));
        return view('communication',compact('number'));
    }
    /**
     * 获取设备列表
     * @param $page 第几页
     * @param $rows 每页显示的条数
     * @param $sort 要排序的字段
     * @param $sortOrder 升序/降序
     * @param $number 设备编号
     * @param $start 开始时间
     * @param $end 结束时间
     */
    public function communicationList()
    {
        $params = Request::instance()->only(['page','rows','sort','sortOrder','number','start','end']);
        $page = intval($params['page']) ? intval($params['page']) : 1;
        $rows = intval($params['rows']) ? intval($params['rows']) : 15;
        $sort = trim($params['sort']) ? trim($params['sort']) : 'id';
        $sortOrder = trim($params['sortOrder']) ? trim($params['sortOrder']) : 'asc';
        $number = trim($params['number']);
        $start = trim($params['start']);
        $end = trim($params['end']);
        return WatchInstructionsRecordModel::communicationList($page,$rows,$sort,$sortOrder,$number,$start,$end);
    }
    /**
     * 下发编号
     */
    public function lowerNumber()
    {
        (new EquipmentVaildate())->lowerNumber();
        return EquipmentService::lowerNumber();
    }
    /**
     * 下发开启检测模式指令
     */
    public function openMonitoring()
    {
        //(new EquipmentVaildate())->lowerNumber();
        return EquipmentService::openMonitoring();
    }
    /**
     * 下发关闭检测模式指令
     */
    public function closeMonitoring()
    {
        (new EquipmentVaildate())->lowerNumber();
        return EquipmentService::closeMonitoring();
    }
}